package model;

import java.util.GregorianCalendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Message extends Persistent implements MessageInterface
{
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int messageId;
	
	@OneToOne(targetEntity=User.class, fetch=FetchType.EAGER)
	@JoinColumn(name="fromUser", nullable=false)
	private UserInterface from;
	
	@OneToOne(targetEntity=User.class, fetch=FetchType.EAGER)
	@JoinColumn(name="toUser", nullable=false)
	private UserInterface to;

	private String subject;
	
	@Column(nullable=false)
	private String contents;
	
	@Column(name="calendar", nullable=false)
	private GregorianCalendar date;
	
	public Message()
	{
		
	}
	
	public Message(UserInterface from)
	{
		this.from = from;
	}
	
	public int getId()
	{
		return messageId;
	}
	
	public UserInterface getFrom()
	{
		return from;
	}
	
	public void setTo(UserInterface to)
	{
		this.to = to;
	}
	
	public UserInterface getTo()
	{
		return to;
	}
	
	public String getSubject()
	{
		if (subject == null || subject.isEmpty())
		{
			return "(No subject)";
		}
		
		return subject;
	}
	
	public void setSubject(String subject)
	{
		this.subject = subject;
	}
	
	public void setContents(String contents)
	{
		this.contents = contents;
	}
	
	public String getContents()
	{
		return contents;
	}

	public void setDate(GregorianCalendar date)
	{
		this.date = date;
	}
	
	public GregorianCalendar getDate()
	{
		return date;
	}
	
	public boolean check()
	{
		// if admin to may be null
		
		return true;
	}
	
	public String toString()
	{
		return getSubject();
	}
}
